package com.yc.happ.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.happ.entity.Elder;
import com.yc.happ.service.ElderService;
import com.yc.happ.serviceImpl.ElderServiceImpl;
import com.yc.happ.util.DBHelper;

@WebServlet("/happinesshome/find/*")
public class FindnameServlet extends BaseServlet{

	

	/**
	 * 
	 */
	private static final long serialVersionUID = -1081328351884603789L;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if("vfind".equals(getRequestFlag(request))){
			doVolunteerFind(request,response);
		}else if("efind".equals(getRequestFlag(request))){
			doElderFind(request,response);
		}
	}

	private void doElderFind(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name=request.getParameter("name");
		String sql="select *from elder where vuname=?";
		List<Object> params=new ArrayList<Object>();
		params.add(0,name);
		double d=DBHelper.doSelectFunt(sql, params);
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(d>0){
			jsonStr="{\"success\":true}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			jsonStr="{\"success\":false}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
		
	}

	private void doVolunteerFind(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name=request.getParameter("name");
		String sql="select *from volunteer where vuname=?";
		List<Object> params=new ArrayList<Object>();
		params.add(0,name);
		double d=DBHelper.doSelectFunt(sql, params);
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(d>0){
			jsonStr="{\"success\":true}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			//失败时，在原页面
			jsonStr="{\"success\":false}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
	}

}
